$.fn.serializeObject = function(){
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

function ajaxDataTable(selector, opts) {
    return $(selector).DataTable($.extend({
        lengthChange: false,
        ordering: false,
        info: true,
        filter: false,
        autoWidth: false,
        paging: true,
        pageLength: 20,
        processing: true,
        serverSide: true
    }, opts));
}

function confirm(message, callback) {
    BootstrapDialog.confirm({
        title: '提示',
        message: message,
        btnCancelLabel: '取消',
        btnOKLabel: '确认',
        closable: true,
        callback: callback
    });
}

function alert(message) {
    BootstrapDialog.alert({
        title: '提示',
        message: message,
        buttonLabel: '关闭',
        closable: true
    });
}

function showDialog(opts) {
    BootstrapDialog.show($.extend({
        cssClass: 'gm-dialog',
        draggable: true,
        closable: true,
        closeByBackdrop: false
    }, opts));
}

function selectList(selector, opts) {
    $(selector).select2($.extend(true, {
        ajax: {
            dataType: 'JSON',
            type: 'POST',
            delay: 200,
            processResults: function (response, params) {
                return {
                    results: response.data,
                    pagination: {
                        more: response.data.length === 0 ? false : response.data.length < response.recordsTotal
                    }
                };
            },
            cache: true
        },
        escapeMarkup: function (markup) { return markup; },
        minimumInputLength: 1,
        language: "zh-CN"
    }, opts));
}

var selection = window.getSelection();
$('.gm').on('click', '.click-selected', function(e) {
    var targetEl = $(this);
    if (!selection.toString()) {
        if (targetEl[0].nodeName.toLowerCase() === 'input') {
            targetEl.select();
        } else {
            selection.selectAllChildren(this);
        }
    }
});

$(function() {
    $(".select2").select2();
});

